package com.icongyou.enterprise.talent_analysis.controller;

import com.icongyou.enterprise.common.result.Result;
import com.icongyou.enterprise.talent_analysis.entity.dto.PortraitTemplateCreateRequest;
import com.icongyou.enterprise.talent_analysis.entity.dto.PortraitTemplateDTO;
import com.icongyou.enterprise.talent_analysis.service.PortraitTemplateService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/portrait")
@RequiredArgsConstructor
public class PortraitTemplateController {

    private final PortraitTemplateService portraitTemplateService;

    @PostMapping("/template")
    public Result<PortraitTemplateDTO> createTemplate(@Valid @RequestBody PortraitTemplateCreateRequest request) {
        return Result.success(portraitTemplateService.createTemplate(request));
    }

    @GetMapping("/template/{id}")
    public Result<PortraitTemplateDTO> getTemplate(@PathVariable("id") String templateId) {
        return Result.success(portraitTemplateService.getTemplate(templateId));
    }

    @PutMapping("/template/{id}")
    public Result<PortraitTemplateDTO> updateTemplate(@PathVariable("id") String templateId, 
                                                      @Valid @RequestBody PortraitTemplateDTO template) {
        return Result.success(portraitTemplateService.updateTemplate(templateId, template));
    }

    @GetMapping("/templates")
    public Result<List<PortraitTemplateDTO>> listTemplates(@RequestParam("enterpriseId") String enterpriseId,
        @RequestParam(value = "active", required = false) Boolean active) {
        try {
            return Result.success(portraitTemplateService.listTemplates(enterpriseId, active));
        } catch (Exception e) {
            return Result.error(500, "查询模板列表失败: " + e.getMessage());
        }
    }    @DeleteMapping("/template/{id}")
    public Result<Void> deleteTemplate(@PathVariable("id") String templateId) {
        portraitTemplateService.deleteTemplate(templateId);
        return Result.success(null);
    }
}
